﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class WebUserControl_uscBaoCaoGiangCap : System.Web.UI.UserControl
{
    string error = "";
    static DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtThang.Text = DateTime.Now.ToString("MM/yyyy");
            LoadTTPP();
        }
        DisplaybtnExport();
    }

    protected bool checkMonth()
    {
        string str = txtThang.Text;
        str = str.Substring(3, 4) + str.Substring(0, 2);
        string daynow = DateTime.Now.ToString("MM/yyyy");
        daynow = daynow.Substring(3, 4) + daynow.Substring(0, 2);
        if (str.Equals(""))
        {
            uscMsgBox1.AddMessage("Tháng không được rỗng.", uscMsgBox.enmMessageType.Error);
            return false;
        }
        else if (str.CompareTo(daynow) == 1)
        {
            uscMsgBox1.AddMessage("Tháng không được lớn hơn tháng hiện tại", uscMsgBox.enmMessageType.Error);
            return false;
        }
        else
            return true;
    }
    private void LoadTTPP()
    {
        try
        {
            drlTTPP.DataSource = new BCDataCenter.BOL.BWBaoCaoGiangCap().dsTTPPList(ref error).Tables[0];
            drlTTPP.DataTextField = "sitename";
            drlTTPP.DataValueField = "SiteCode";
            drlTTPP.DataBind();
        }
        catch
        {

        }
    }
    private void BindReport()
    {
        try
        {
            dt = new BCDataCenter.BOL.BWBaoCaoGiangCap().dsReport(ref error, txtThang.Text, drlTTPP.SelectedItem.Value, Session["UserName"].ToString()).Tables[0];
            int n = dt.Columns.Count;
            if (n > 22)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 22; j < n; j++)
                    {
                        if (dt.Rows[i][j].ToString() != "")
                        {
                            dt.Rows[i][j] = "X";
                        }
                    }
                }
            }
            gvBaoCaoDanhHieu.AutoGenerateColumns = true;
            gvBaoCaoDanhHieu.DataSource = dt;
            gvBaoCaoDanhHieu.DataBind();
            DisplaybtnExport();
        }
        catch (Exception ex)
        {
            dt.Rows.Clear();
            gvBaoCaoDanhHieu.DataSource = dt;
            gvBaoCaoDanhHieu.DataBind();
            DisplaybtnExport();
            uscMsgBox1.AddMessage("Không có dữ liệu.", uscMsgBox.enmMessageType.Info);
        }
    }
    private void DisplaybtnExport()
    {
        if (dt.Rows.Count > 0)
        {
            btnExportExcel.Visible = true;
        }
        else
        {
            btnExportExcel.Visible = false;
        }
    }

    protected void btnViewReport_Click(object sender, EventArgs e)
    {
        BindReport();
    }
    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
        string filename = "BaoCaoGiangCap_" + DateTime.Now.ToString("ddMMyyyy_HHmm") + ".xls";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        //System.Text.Encoding.UTF8
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", filename));
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        string style = @"<style> .text { mso-number-format:\@;} </script> ";
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                GridView gr = new GridView();
                DataTable dtExport = dt;
                gr.DataSource = dtExport;
                gr.DataBind();
                for (int i = 0; i < dtExport.Rows.Count; i++)
                {
                    //dtExport.Rows[i][2] = "'" + dtExport.Rows[i][2].ToString();
                    //dtExport.Rows[i][4] = "'" + dtExport.Rows[i][4].ToString();
                    //dtExport.Rows[i][5] = "'" + dtExport.Rows[i][5].ToString();
                    //dtExport.Rows[i][7] = "'" + dtExport.Rows[i][7].ToString();
                    gr.Rows[i].Cells[2].Attributes.Add("class", "text");
                    gr.Rows[i].Cells[4].Attributes.Add("class", "text");
                    gr.Rows[i].Cells[5].Attributes.Add("class", "text");
                    gr.Rows[i].Cells[7].Attributes.Add("class", "text");
                }                

                Label lableTitle = new Label();
                lableTitle.Text = "BAO CAO GIANG CAP";
                lableTitle.Font.Bold = true;
                lableTitle.Font.Size = FontUnit.XXLarge;
                Label lblThang = new Label();
                lblThang.Text = "Thang " + txtThang.Text;
                lblThang.Font.Bold = true;
                Label lblNgayGio = new Label();
                lblNgayGio.Text = "Ngay gio truy xuat " + DateTime.Now.ToString("dd/MM/yyyy HH:mm");
                lblNgayGio.Font.Italic = true;
                Table tb = new Table();
                TableRow tr1 = new TableRow();
                TableCell td1 = new TableCell();
                td1.ColumnSpan = 4;
                td1.Width = Unit.Pixel(200);
                td1.Controls.Add(lableTitle);

                TableCell td2 = new TableCell();
                td2.Controls.Add(lblThang);
                TableCell td3 = new TableCell();
                td3.ColumnSpan = 4;
                td3.Controls.Add(lblNgayGio);
                TableRow tr2 = new TableRow();
                TableRow tr3 = new TableRow();

                tr1.Cells.Add(td1);
                tr2.Cells.Add(td2);
                tr3.Cells.Add(td3);
                tb.Rows.Add(tr1);
                tb.Rows.Add(tr2);
                tb.Rows.Add(tr3);
                form.Controls.Add(tb);
                form.Controls.Add(gr);
                this.Controls.Add(form);

                form.RenderControl(htw);

                // Style is added dynamically
                HttpContext.Current.Response.Write(style); 
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }

    }    
}
